package cn.itcast.service.impl;

import cn.itcast.mapper.OrderMapper;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @program: schedule
 * @description: 业务处理, 超时订单处理
 * @author: zhanghz001
 * @create: 2021-08-05 16:21
 **/
// @Service
public class ZhzOrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    public static final int ORDER_TIME_OUT = 10;
    public static final int PAGE = 1;
    public static final int SIZE = 10;
    
    @Override
    public List<Order> findOrderTimeOut(int item, int totalSharingCount) {
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria(); //封装条件
        
        //订单状态0，支付状态0 ，下单时间超过10分钟 ID % totalSharingCount = item;
        criteria.andEqualTo("orderStatus", "0");
        criteria.andEqualTo("payStatus", "0");
        
        //下单时间 < 当前时间 - 10
        Date date = DateUtils.addMinutes(new Date(), -ORDER_TIME_OUT);
        criteria.andLessThan("createTime", date);
        criteria.andCondition("id % " + totalSharingCount + "=" + item);
        example.orderBy("createTime").asc();
        PageHelper.startPage(PAGE, SIZE);
        List<Order> orders = orderMapper.selectByExample(example);//根据条件查询订单数据
        return orders;
    }
    
    @Override
    public void updateStatus(Order order, String status) {
        Long id = order.getId();
        String orderStatus = order.getOrderStatus();
        
        order.setOrderStatus("4");//订单取消
        order.setUpdateTime(new Date()); //设置订单更新时间
        
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        criteria.andEqualTo("orderStatus", orderStatus);
        orderMapper.updateByExample(order, example);
    }
    
    @Override
    public void countOrder(Date startDate, Date endDate) {
        
    }
}
